//
//  AY_GenderViewController.m
//  AiYue
//
//  Created by mac on 2023/12/26.
//

#import "AY_GenderViewController.h"
#import "AY_MainTabViewController.h"
#import "AppDelegate.h"

@interface AY_GenderViewController ()

@property (nonatomic, strong) UIImageView *manIcon;
@property (nonatomic, strong) UIImageView *womanIcon;
@end

@implementation AY_GenderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.gk_navBarAlpha = 0;
    UIImageView *topImg = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"AY_gender_top") andBg:nil andRadius:0 andAddSubView:self.view];
    [self.view addSubview:topImg];
    [topImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.mas_equalTo(YNUM(210));
    }];
    
    UILabel *infoLabel2 = [AYTool createLabelWithFrame:CGRectZero andText:@"选择性别，进入您的专属小说世界" andTextColor:COLOR(@"666666") andBgColor:nil andFont:YFont(12) andLines:1 andRadius:0 andAddSubView:topImg];
    [infoLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topImg).offset(YNUM(50));
        make.bottom.equalTo(topImg).offset(-YNUM(10));
    }];
    
    UILabel *infoLabel1 = [AYTool createLabelWithFrame:CGRectZero andText:@"优质小说任您挑选" andTextColor:COLOR(@"333333") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(24)] andLines:1 andRadius:0 andAddSubView:topImg];
    [infoLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topImg).offset(YNUM(50));
        make.bottom.equalTo(infoLabel2.mas_top).offset(-YNUM(10));
    }];
    
    UIButton *man = [AYTool createButtonWithFrame:CGRectZero andTitle:nil andFont:nil andTitleColor:nil andBg:nil andImage:nil andBgImage:YImage(@"AY_man") andTarget:self andAction:@selector(genderAction:) andRadius:0 andAddSubView:self.view];
    man.adjustsImageWhenHighlighted = NO;
    man.tag = 1;
    [man mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topImg.mas_bottom).offset(YNUM(50));
        make.right.equalTo(self.view.mas_centerX).offset(-YNUM(20));
        make.width.mas_equalTo(YNUM(110));
        make.height.mas_equalTo(YNUM(150));
    }];
    
    self.manIcon = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"AY_gender_choose") andBg:nil andRadius:0 andAddSubView:self.view];
    [self.manIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(man);
        make.top.equalTo(man.mas_bottom).offset(YNUM(16));
        make.width.height.mas_equalTo(YNUM(24));
    }];
    
    UIButton *woman = [AYTool createButtonWithFrame:CGRectZero andTitle:nil andFont:nil andTitleColor:nil andBg:nil andImage:nil andBgImage:YImage(@"AY_woman") andTarget:self andAction:@selector(genderAction:) andRadius:0 andAddSubView:self.view];
    woman.tag = 2;
    woman.adjustsImageWhenHighlighted = NO;
    [woman mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topImg.mas_bottom).offset(YNUM(50));
        make.left.equalTo(self.view.mas_centerX).offset(YNUM(20));
        make.width.mas_equalTo(YNUM(110));
        make.height.mas_equalTo(YNUM(150));
    }];
    
    self.womanIcon = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"AY_gender_choose") andBg:nil andRadius:0 andAddSubView:self.view];
    [self.womanIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(woman);
        make.top.equalTo(woman.mas_bottom).offset(YNUM(16));
        make.width.height.mas_equalTo(YNUM(24));
    }];
    
    if ([AYMMKV getInt32ForKey:GENDER defaultValue:1] == 1) {
        self.manIcon.hidden = NO;
        self.womanIcon.hidden = YES;
    }else {
        self.manIcon.hidden = YES;
        self.womanIcon.hidden = NO;
    }
    
    UIButton *done = [AYTool createButtonWithFrame:CGRectZero andTitle:@"确认选择" andFont:YFont(16) andTitleColor:[UIColor whiteColor] andBg:THEME_COLOR andImage:nil andBgImage:nil andTarget:self andAction:@selector(doneAction) andRadius:YNUM(16) andAddSubView:self.view];
    [done mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(YNUM(65));
        make.right.equalTo(self.view).offset(-YNUM(65));
        make.bottom.equalTo(self.view).offset(-YNUM(100));
        make.height.mas_equalTo(YNUM(65));
    }];
}

- (void)genderAction:(UIButton *)btn {
    if (btn.tag == 1) {
        self.manIcon.hidden = NO;
        self.womanIcon.hidden = YES;
        [AYMMKV setUInt32:1 forKey:GENDER];
    }else {
        self.womanIcon.hidden = NO;
        self.manIcon.hidden = YES;
        [AYMMKV setUInt32:2 forKey:GENDER];
    }
}

- (void)doneAction {
    if ([self.pageType isEqual:@"mine"]) {
        [self.navigationController popViewControllerAnimated:YES];
    }else {
        [AYApiRequest genderWithFavoriteType:[NSString stringWithFormat:@"%d",[AYMMKV getInt32ForKey:GENDER defaultValue:1]] Success:^(AY_BaseModel * _Nonnull pkg) {
            AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
            AY_MainTabViewController *TabBar = [[AY_MainTabViewController alloc] init];
            UINavigationController *nav = [UINavigationController rootVC:TabBar translationScale:NO];
            app.window.rootViewController = nav;
            [AYMMKV setBool:NO forKey:ISFIRST];
        } failure:^(int status, NSString * _Nonnull info) {
            ShowToast(@"亲，网络不顺畅哟~");
        }];
    }
}
@end
